package mse.eai.esb;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import messageObjects.BalanceObj;
import messageObjects.MovementObj;

import org.jboss.logging.Logger;
import org.jboss.soa.esb.actions.ActionLifecycle;
import org.jboss.soa.esb.actions.ActionLifecycleException;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class ParseBalance implements ActionLifecycle {

	private static final Logger log = Logger.getLogger(ParseBalance.class);

	@Override
	public void destroy() throws ActionLifecycleException {
		// TODO Auto-generated method stub

	}

	@Override
	public void initialise() throws ActionLifecycleException {
		// TODO Auto-generated method stub

	}

	public ParseBalance(ConfigTree config) {

	}

	public Message process(Message m) {
		log.info("ESB Action start ------>");

		try {
			log.info("ParseBalance");

			String xml = (String) m.getBody().get(
					"org.jboss.soa.esb.message.defaultEntry");

			InputSource inputSource = new InputSource(new StringReader(xml));

			XPath xPath = XPathFactory.newInstance().newXPath();

			// XPathExpression
			// xPathExpression=xPath.compile("/catalog/journal/article[@date='January-2004']/title");
			// String title = xPathExpression.evaluate(inputSource);

			String expression = "//return";
			NodeList nodes = (NodeList) xPath.evaluate(expression, inputSource,
					XPathConstants.NODESET);

			String balance = "";
			List<MovementObj> movements = new ArrayList<MovementObj>();

			log.info("Parsing balance"); // in " + xml);

			for (int i = 0; i < nodes.getLength(); i++) {

				Node node = nodes.item(i);
				Node balanceNode = (Node) xPath.evaluate("lindenEuros", node,
						XPathConstants.NODE);

				log.debug("NAME: " + balanceNode.getNodeName());
				log.debug("VALUE: " + balanceNode.getTextContent());

				balance = balanceNode.getTextContent();

				log.debug("BALANCE: " + balance);

				NodeList nodesMovements = (NodeList) xPath.evaluate(
						"movements", node, XPathConstants.NODESET);

				log.debug("HOW MANY MOVMENTS: " + nodesMovements.getLength());

				for (int j = 0; i < nodesMovements.getLength(); i++) {
					Node nodeMovement = nodesMovements.item(i);

					log.debug("NAME: " + nodeMovement.getNodeName());
					log.debug("VALUE: " + nodeMovement.getTextContent());

					Node dateNode = (Node) xPath.evaluate("date", nodeMovement,
							XPathConstants.NODE);
					Node amountNode = (Node) xPath.evaluate("lindenEuros",
							nodeMovement, XPathConstants.NODE);
					Node amountType = (Node) xPath.evaluate(
							"lindenmovementstype", nodeMovement,
							XPathConstants.NODE);

					Node moveDescNode = (Node) xPath.evaluate("description",
							amountType, XPathConstants.NODE);

					MovementObj mov = new MovementObj(
							dateNode.getTextContent(),
							moveDescNode.getTextContent(),
							amountNode.getTextContent());
					movements.add(mov);

				}

			}

			BalanceObj b = new BalanceObj(balance, movements);
			m.getBody().add("balance", b);

			log.info("Done");

			log.info("<------- ESB Action done");
			return m;

		} catch (Exception e) {
			log.error(e);
			log.info("<------- ESB Action done");
			return m;
		}
	}

}
